/** @format */

import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import homeData from './module/homeData'
import myData from './module/myData'

import {
  getIndustryList,
  getIndustryPositionList,
  searchAllJob,
  searchAccountAllJob,
  uploadToken,
  getUserJobReportList,
  getUserJobReportDetail
} from '@/api/common'
import {
  getValidationCode,
  companyLoginByValidationCode,
  loginByValidationCode
} from '@/api/login'
import { getUserInfo } from '@/api/user'

// import { noAccessToken } from '../utils/userAuth'
import { setUserInfo, setRealName } from '@/utils/userAuth'

Vue.use(Vuex)

export default new Vuex.Store({
  strict: false,
  state: {
    // url: encodeURIComponent(location.href.split('#')[0])
    url: window.location.href.split('#')[0],
    userInfo: null,
    industryList: [],
    industryPositionList: [],
    allJob: [],
    userJobReportList: [],
    userJobReportDetail: {}
  },
  actions: {
    async companyLoginByValidationCode({ dispatch }, { mobile, code }) {
      const response = await companyLoginByValidationCode({ mobile, code })
      setUserInfo(response.token)
      return response
    },
    async loginByValidationCode({ dispatch }, { mobile, code }) {
      const response = await loginByValidationCode({ mobile, code })
      setUserInfo(response)
      dispatch('getUserInfo')
      return response
    },
    async getUserInfo() {
      const result = await getUserInfo()
      setRealName(result)
    },
    async getIndustryList({ commit }) {
      let result = await getIndustryList()
      result.unshift({ name: '全部', id: '' })
      commit('setIndustryList', result)
    },
    async getIndustryPositionList({ commit }, industryId) {
      let result = await getIndustryPositionList({ industryId: industryId })
      result.unshift({ name: '全部', id: '' })
      commit('setIndustryPositionList', result)
    },
    async searchAllJob({ commit }, { industryId, positionId, title }) {
      let result = await searchAllJob({
        industryId,
        positionId,
        title
      })
      commit('setSearchAllJob', result.content)
    },
    async searchAccountAllJob({ commit }, { industryId, positionId, title }) {
      let result = await searchAccountAllJob({
        industryId,
        positionId,
        title
      })
      commit('setSearchAllJob', result.content)
    },
    async getValidationCode({ commit }, mobile) {
      let result = await getValidationCode({
        mobile
      })
      return result
    },
    async getUploadToken({ commit }) {
      let result = await uploadToken()
      commit('setSearchAllJob', result.content)
    },
    async getUserJobReportList({ commit }, userJobId) {
      let result = await getUserJobReportList({ userJobId })
      commit('setUserJobReportList', result)
    },
    async getUserJobReportDetail({ commit }, userJobReportId) {
      let result = await getUserJobReportDetail({ userJobReportId })
      commit('setUserJobReportDetail', result)
    }
  },
  mutations: {
    saveUserInfo(state, { userInfo }) {
      state.userInfo = userInfo
    },
    setIndustryList(state, industryList) {
      state.industryList = industryList
    },
    setIndustryPositionList(state, industryPositionList) {
      state.industryPositionList = industryPositionList
    },
    setSearchAllJob(state, allJob) {
      state.allJob = allJob
    },
    setUploadToken(state, uploadToken) {
      state.uploadToken = uploadToken
    },
    setUserJobReportList(state, userJobReportList) {
      state.userJobReportList = userJobReportList
    },
    setUserJobReportDetail(state, userJobReportDetail) {
      state.userJobReportDetail = userJobReportDetail
    }
  },
  modules: {
    homeData,
    myData
  },
  plugins: [
    // 数据全部持久化
    createPersistedState({
      storage: window.localStorage
    })
  ]
})
